new Vue({
    el: '#app',
    data() {
        return {
            html: '',
            options: [{
                value: 0,
                label: '协议管理'
            }
            ],
            form: {
                User_Agreement_SerialNumber: "",
                User_Agreement_Title: '',
                User_Agreement_Main: "",
                Enterprise_Info_SerialNumber: $.cookie("enterprise"),
            },
            loading: false,
            rules: {
                User_Agreement_Title: [
                    { required: true, message: '请输入标题', trigger: 'blur' },
                ]
            }
        }
    },
    mounted() {
        this.getNews()
    },
    methods: {
        getNews() {
            const { createEditor, createToolbar } = window.wangEditor
            const this_ = this
            const editorConfig = {
                placeholder: '协议内容',
                onChange(editor) {
                    this_.html = editor.getHtml()
                    console.log(this_.html)
                    // 也可以同步到 <textarea>
                }
            }
            const editor = createEditor({
                selector: '#editor-container',
                html: '<p><br></p>',
                config: editorConfig,
                mode: 'simple', // or 'simple'
            })

            const toolbarConfig = {}

            const toolbar = createToolbar({
                editor,
                selector: '#toolbar-container',
                config: toolbarConfig,
                mode: 'default', // or 'simple'
            })
        },
        back() {
            window.history.go(-1)
        },
        subMessage() {
            const _this = this
            this.$refs['form'].validate((valid) => {
                if (valid) {
                    if (!/\S/.test(this.html.User_Agreement_Main)) {
                        this.$message.error("请输入协议内容")
                        return
                    }
                    this.form.User_Agreement_Main = this.html
                    _this.loading = true
                    $.ajax({
                        url: DomainName + '/getAddAction?token=' + $.cookie("token"),
                        type: 'POST',
                        data: {
                            Data: JSON.stringify({
                                user_agreement: [_this.form]
                            })
                        },
                        success: function (res) {
                            console.log('提交结果', res)
                            if (res.Status == 'success') {
                                _this.$message.success("发布成功!")
                                window.history.go(-1)
                            }
                        },
                        error: function () {
                            _this.$message.error("操作失败，请稍后再试")
                        },
                        complete: function (XMLHttpRequest) {
                            _this.loading = false
                            if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
                                window.parent.document.location.href = "../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
                            }
                        }
                    })
                }
            })

        }
    },
})